package com.hudongwx.food.fargment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.LoginFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.hudongwx.food.R;
import com.hudongwx.food.utils.MyJsonObject;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.core.BitmapSize;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * 推荐
 * A simple {@link Fragment} subclass.
 */
@ContentView(R.layout.fragment_recommend)
public class RecommendFragment extends Fragment {
    private String TAG = "RecommendFragment";
    private View view;
    @ViewInject(R.id.RecommendFragment_list2)
    private RecyclerView recyclerView;
    @ViewInject(R.id.RecommendFragment_list1)
    private ListView listView;
    private JSONArray array;
    private MyListAdapter myListAdapter;
    private MyRecommendAdapter myRecommendAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null == view) {
            view = x.view().inject(this, inflater, container);
            GridLayoutManager manager = new GridLayoutManager(getActivity(), 1);
            recyclerView.setLayoutManager(manager);
            myRecommendAdapter = new MyRecommendAdapter(array);
            recyclerView.setAdapter(myRecommendAdapter);
            myListAdapter = new MyListAdapter(array);
            listView.setAdapter(myListAdapter);
            getitem();
            getitem1();
        }
        return view;
    }

    class MyRecommendAdapter extends RecyclerView.Adapter<MyRecommendViewHolder> {
        JSONArray listView2;

        public MyRecommendAdapter(JSONArray listView2) {

            this.listView2 = listView2;
        }


        public void updateListView2(JSONArray listView2) {
            this.listView2 = listView2;
            notifyDataSetChanged();
        }

        @Override
        public MyRecommendViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view1 = LayoutInflater.from(getActivity()).inflate(R.layout.recommen_recommend_2, null);
            return new MyRecommendViewHolder(view1);
        }

        @Override
        public void onBindViewHolder(MyRecommendViewHolder holder, int position) {

            if (null == listView2) {
                recyclerView.setVisibility(View.GONE);
            } else {
                recyclerView.setVisibility(View.VISIBLE);
                try {
                    holder.tv.setText(listView2.getJSONObject(position).getString("Title"));
                    JSONArray array2 = listView2.getJSONObject(position).getJSONArray("ListFile");
                    BitmapUtils utils = new BitmapUtils(getActivity());
                    BitmapDisplayConfig config = new BitmapDisplayConfig();
                    if (array2.length()==1){
                        String url = array2.getJSONObject(0).getString("Url");
                        utils.display(holder.iv1,url,config);
                    }

                    else if (array2.length()==2){
                        String url = array2.getJSONObject(0).getString("Url");
                        String url1 = array2.getJSONObject(1).getString("Url");
                        utils.display(holder.iv1,url,config);
                        utils.display(holder.iv2,url1,config);
                    }else if (array2.length()>=3){
                        String url = array2.getJSONObject(0).getString("Url");
                        String url1 = array2.getJSONObject(1).getString("Url");
                        String url2 = array2.getJSONObject(2).getString("Url");
                        utils.display(holder.iv1,url,config);
                        utils.display(holder.iv2,url1,config);
                        utils.display(holder.iv3,url2,config);
                    }else if (array2.length()==0){
                        holder.lin.setVisibility(View.GONE);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public int getItemCount() {
            if (null==listView2)return 0;
            return listView2.length();
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }
    }

    class MyRecommendViewHolder extends RecyclerView.ViewHolder {
       private TextView tv;
        private LinearLayout lin;
        private ImageView iv1,iv2,iv3;
        public MyRecommendViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.Recommend_tv);
            lin = (LinearLayout) itemView.findViewById(R.id.lin);
            iv1 = (ImageView) itemView.findViewById(R.id.recimg_1);
            iv2 = (ImageView) itemView.findViewById(R.id.recimg_2);
            iv3= (ImageView) itemView.findViewById(R.id.recimg_3);
        }
    }

    public void getitem() {

        RequestParams params = new RequestParams("http://api.shiandianping.com/api/sourceAndroidDianPing/v3.1.0/posts");
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    myRecommendAdapter.updateListView2(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
  class MyListAdapter extends BaseAdapter{
      JSONArray array1;
      public MyListAdapter(JSONArray array1) {
          this.array1 = array1;
      }


      public void item(JSONArray array1){
          this.array1 = array1;
          notifyDataSetChanged();
      }
      @Override
      public int getCount() {
          if (array1 == null)
              return 0;
          return array1.length();
      }

      @Override
      public Object getItem(int position) {
          return getItem(position);
      }

      @Override
      public long getItemId(int position) {
          return position;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          MyViewHolder myViewHolder;
          if (null==array1)return null;
          if (null==convertView){
              myViewHolder = new MyViewHolder();
              convertView = LayoutInflater.from(getActivity()).inflate(R.layout.recommen_recommend_1,null);
              myViewHolder.tv1 = (TextView) convertView.findViewById(R.id.Recommend_tv_1);
              myViewHolder.iv = (ImageView) convertView.findViewById(R.id.Recommend_iv_1);
              myViewHolder.tv2 = (TextView) convertView.findViewById(R.id.Recommend_tv_2);
              convertView.setTag(myViewHolder);
          }else {
              myViewHolder = (MyViewHolder) convertView.getTag();
          }
          try {
              myViewHolder.tv1.setText(array1.getJSONObject(position).getString("Title"));
              Log.i(TAG, "getView: --------"+myViewHolder.tv1);
              x.image().bind(myViewHolder.iv,array1.getJSONObject(position).getString("CoverImg"));
              Log.i(TAG, "getView: --------"+myViewHolder.iv);
              myViewHolder.tv2.setText(array1.getJSONObject(position).getString("BoardName"));
              Log.i(TAG, "getView: --------"+myViewHolder.tv2);
          } catch (JSONException e) {
              e.printStackTrace();
          }
          return convertView;
      }
      class MyViewHolder{
          TextView tv1,tv2;
          ImageView iv;
      }
  }
    public void getitem1() {
//http://api.shiandianping.com/api/sourceAndroidDianPing/v3.1.0/posts
        RequestParams params = new RequestParams("http://api.shiandianping.com/api/sourceAndroidDianPing/v3.1.0/posts/GetGoodPost");
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    Log.i(TAG, "onSuccess: ----------------------123下载成功" + data.length());
                    myListAdapter.item(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e(TAG, "onError: ..................." + ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.i(TAG, "onCancelled: >>>>>>>>>>>>>>>>>>>>>>" + cex);
            }

            @Override
            public void onFinished() {
                Log.e(TAG, "onFinished: 11111111111111111111111111111");
            }
        });
    }
}
